package com.perfma.xtest.tool.word;

import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author: junhui.si
 * @date: 2020-11-16 16:49
 **/
public class CollectionUtil {
    public static <K, V> Map<K, V> newHashMap(Pair<K, V>... pairs) {
        Map<K, V> map = new HashMap<K, V>();
        if (pairs != null && pairs.length > 0) {
            for (Pair<K, V> p : pairs) {
                map.put(p.getLeft(), p.getRight());
            }
        }
        return map;
    }

    public static <K, V> Map<K, V> newHashMap(List<Pair<K, V>> pairs) {
        Map<K, V> map = new HashMap<K, V>();
        if (pairs != null && pairs.size() > 0) {
            for (Pair<K, V> p : pairs) {
                map.put(p.getLeft(), p.getRight());
            }
        }
        return map;
    }

    public static boolean isEmpty(Map map) {
        return map == null || map.size() == 0;
    }

    public static boolean isEmpty(Collection collection) {
        return collection == null || collection.size() == 0;
    }


    public static List wrap(List t) {
        return isEmpty(t) ? Collections.EMPTY_LIST : t;
    }

    public static boolean isNotEmpty(Collection collection) {
        return !isEmpty(collection);
    }

    public static boolean isNotEmpty(Map map) {
        return !isEmpty(map);
    }

    public static <T> boolean isNotEmpty(T[] array) {
        return array != null && array.length > 0;
    }

    public static <T> String concat(Collection<T> ids) {
        StringBuilder sb = new StringBuilder();
        if (isEmpty(ids)) {
            return "";
        }
        for (T i : ids) {
            sb.append(i).append(",");
        }
        return sb.substring(0, sb.length() - 1);
    }

    public static List<Integer> convert(String[] s) {
        if (s == null || s.length == 0) {
            return new ArrayList<>();
        }
        List<Integer> list = new ArrayList<>(s.length);
        for (String each : s) {
            list.add(Integer.parseInt(each));
        }
        return list;
    }

    public static <T> List<T> split2Any(String source, String split, Convert<T> convert) {
        List list = new ArrayList();
        if (!StringUtils.isEmpty(source) && !StringUtils.isEmpty(split)) {
            String[] ss = source.split(split);
            String[] var5 = ss;
            int var6 = ss.length;

            for (int var7 = 0; var7 < var6; ++var7) {
                String s = var5[var7];
                list.add(convert.convert(s));
            }

            return list;
        } else {
            return list;
        }
    }

    public static <T> String concat(T[] ids) {
        StringBuilder sb = new StringBuilder();
        if (ids == null || ids.length == 0) {
            return "";
        }
        for (T i : ids) {
            sb.append(i).append(",");
        }
        return sb.substring(0, sb.length() - 1);
    }

    public static List<String> split2String(String source, String split) {
        List<String> list = new ArrayList();
        if (StringUtils.isEmpty(source) || StringUtils.isEmpty(split)) {
            return list;
        }

        String[] ss = source.split(split);
        return Arrays.asList(ss);
    }

    public static List<Integer> split2Integer(String source, String split) {
        List<Integer> list = new ArrayList();
        if (StringUtils.isEmpty(source) || StringUtils.isEmpty(split)) {
            return list;
        }

        String[] ss = source.split(split);
        for (String s : ss) {
            list.add(Integer.parseInt(s));
        }
        return list;
    }
}
